package stsj.meddroid.elements;

import java.util.Random;

import stsj.meddroid.Resources;
import stsj.meddroid.screen.BattleScreen;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;

public class SMC extends Enemy {
	private int state;
	private int count;
	private int counter;
	private int counterAttack;
	public static boolean isBertelur;
	private static int se;
	private static int ye;
	private boolean encounter;
	private static Random random;
	public SMC(int x, int y) {
		super(250, Enemy.SMC1495, x, y, Resources.angka);
		// TODO Auto-generated constructor stub
		mySprite = new Sprite(BattleScreen.texBlack4,0,0,167,200);
		mySprite.setSize(167, 200);
		mySprite.setPosition(x, y);
		myRectangle = new Rectangle(x,y,mySprite.getWidth(),mySprite.getHeight());
		state = 100;
		count = 0;
		counter = 0;
		counterAttack = 0;
		isBertelur = false;
		random = new Random();
		encounter = false;
	}

	@Override
	public void damaged() {
		// TODO Auto-generated method stub
		super.damaged(1);
	}

	@Override
	public void dropOrb() {
		// TODO Auto-generated method stub
		super.dropOrb(80);
	}

	@Override
	public void lasered() {
		// TODO Auto-generated method stub
		super.lasered(25);
	}
	
	public void moveRight(){
		super.moveRight();
		state++;
		mySprite.setScale(1, 1);
		mySprite.setRegion(((state/4)%4)*300,0, 167,200);
	}
	
	public void moveLeft(){
		super.moveLeft();
		state++;
		mySprite.setRegion(((state/4)%4)*300,0, 167,200);
		mySprite.setScale(-1, 1);
	}
	
	public void moveUp() {
		super.moveUp();
		state++;
		mySprite.setRegion(((state/4)%4)*300,0, 167,200);
	}
	
	public void moveDown() {
		super.moveDown();
		state++;
		mySprite.setRegion(((state/4)%4)*300,0, 167,200);	
	}
	
	public void autoMove(){
		if (count != 6) {count++;}
		else{
				if (state <= 175){moveRight();}
				else if(state <= 325){moveUp();}
				else if(state <= 500){moveLeft();}
				else{
					moveDown(); 
					if (state == 150){state = 0;}
				}
//				if (Math.abs(BattleScreen.centerX - x) <= 240 && Math.abs(BattleScreen.centerY - y) <= 160) {
//					if (BattleScreen.centerX < x) {
//						moveLeft();
//					} else {
//						moveRight();
//					}
//					if (BattleScreen.centerY < y) {
//						moveDown();
//					} else {
//						moveUp();
//					}
//				} else {
//					if (state < 30){				
//						face = RIGHT;
//						moveRight();
//					}
//					else{
//						face = LEFT;
//						moveLeft();
//					}
//				}
//			}
			
			if (counter == 225) {
				isBertelur = true;
				se = x;
				ye = y;
				counter = 0;
			}
			counter++;
			count = 0;
			
			if (state >= 1000) {
				state = 0;
			}
			
			if (counterAttack != 22) {counterAttack++;}
			else {
				if (Math.abs(BattleScreen.centerX - x) <= 45 && Math.abs(BattleScreen.centerY - y) <= 55 ||
					Math.abs(BattleScreen.centerX - x) <= 45 && Math.abs(BattleScreen.centerY - (y+mySprite.getHeight())) <= 55 ||
					Math.abs(BattleScreen.centerX - (x+mySprite.getWidth())) <= 45 && Math.abs(BattleScreen.centerY - y) <= 55 ||
					Math.abs(BattleScreen.centerX - (x+mySprite.getWidth())) <= 45 && Math.abs(BattleScreen.centerY - (y+mySprite.getHeight())) <= 55	
					) {
						BattleScreen.medDroid.damaged(65);
				}
				counterAttack = 0;
			}
		}
	}
	
	public static void addEnemySlime() {
		int r = random.nextInt(3);
		if (r == 0) {
			HTN temp = new HTN(se+83,ye+100);
			temp.encounter = true;
			BattleScreen.listOfEnemies.put(temp,false);
		} else if (r == 1) {
			SPT temp = new SPT(se+83,ye+100);
			temp.encounter = true;
			BattleScreen.listOfEnemies.put(temp,false);
		} else if (r == 2) {
			RBJ temp = new RBJ(se+83,ye+100);
			temp.encounter = true;
			BattleScreen.listOfEnemies.put(temp,false);
		}
	}
	

}
